package com.sc;

import org.junit.Test;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

/**
 * Created by Shen Chao on 2018-06-07-007.
 */
public class ListPaiXu {
    @Test
    public void listPauxu1() {
        List<Integer> list = new ArrayList<>();
        list.add(2);
        list.add(5);
        list.add(1);
        list.add(5);
        list.add(4);
        list.add(3);
        System.out.println(list);
        Collections.sort(list);
        System.out.println(list);
    }

    public class User implements Comparable<User> {
        private int score;
        private int age;

        private User(int score, int age) {
            super();
            this.score = score;
            this.age = age;
        }

        private int getScore() {
            return score;
        }

        private int getAge() {
            return age;
        }

        @Override
        public int compareTo(User o) {
            int i = this.getAge() - o.getAge();
            if (i == 0) {
                return this.score - o.getScore();
            }
            return i;
        }
    }

    @Test
    public void ListPaixu2() {
        List<User> users = new ArrayList<>();
        users.add(new User(78, 26));
        users.add(new User(67, 23));
        users.add(new User(34, 56));
        users.add(new User(55, 23));
        Collections.sort(users);
        for (User user : users) {
            System.out.println(user.getScore() + "," + user.getAge());
        }
    }

    public class Students {

        private int age;
        private int score;

        private Students(int age, int score) {
            super();
            this.age = age;
            this.score = score;
        }

        public int getAge() {
            return age;
        }

        public int getScore() {
            return score;
        }
    }

    @Test
    public void listPaixu3() {
        List<Students> students = new ArrayList<>();
        students.add(new Students(23, 100));
        students.add(new Students(27, 98));
        students.add(new Students(29, 99));
        students.add(new Students(29, 98));
        students.add(new Students(22, 89));

        Collections.sort(students, new Comparator<Students>() {
            @Override
            public int compare(Students o1, Students o2) {
                int i = o1.getScore() - o2.getScore();
                if (i == 0) {
                    return o1.getAge() - o2.getAge();
                }
                return i;
            }
        });
        System.out.println(" Collections.sort");
        for (Students stu : students) {
            System.out.println("score:" + stu.getScore() + ":age" + stu.getAge());
        }
        //lambda
        Collections.sort(students, (o1, o2) -> {
            int i = o1.getAge() - o2.getAge();
            if (i == 0) {
                return o1.getScore() - o2.getScore();
            }
            return i;
        });
        System.out.println("lambda");
        for (Students stu : students) {
            System.out.println("score:" + stu.getScore() + ":age" + stu.getAge());
        }
        //list.sort
        students.sort((o1, o2) -> {
            int i = o1.getScore() - o2.getScore();
            if (i == 0) {
                return o1.getAge() - o2.getAge();
            }

            return i;
        });
        System.out.println("list.sort");
        for (Students stu : students) {
            System.out.println("score:" + stu.getScore() + ":age" + stu.getAge());
        }
    }
}
